package org.papiez.guice.extension;

import static org.apache.commons.lang.StringUtils.capitalize;
import static org.apache.commons.lang.StringUtils.join;
import static org.apache.commons.lang.StringUtils.split;

import org.apache.commons.lang.Validate;

final class ChainName {
	private static final Character DOT = '.';

	private static final String CHAINED_WITH_STRING = "$$ChainedWith";

	private final Class<?> chainClass;

	private ChainName(Class<?> chainClass) {
		Validate.notNull(chainClass, "");
		this.chainClass = chainClass;
	}

	public static ChainName forClass(Class<?> chainClass) {
		return new ChainName(chainClass);
	}

	public String chainedWith(Class<?> withClass) {
		Validate.notNull(withClass, "");

		String[] withClassPath = split(withClass.getName(), DOT);
		for (int i = 0; i < withClassPath.length; i++) {
			withClassPath[i] = capitalize(withClassPath[i]);
		}

		return chainClass.getName() + CHAINED_WITH_STRING + join(withClassPath);
	}
}
